using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Prism.Navigation.Regions;
using WpfAppPrismT1.Services;
using WpfAppPrismT1.ViewEvents;
using WpfAppPrismT1.Views;

namespace WpfAppPrismT1.ViewModels;

internal partial class ShellViewModel : ObservableObject
{
    public ShellViewModel(IRegionManager regionManager) { }

    [RelayCommand]
    public void ShowPage1()
    {
        var regionManager = App.AppContainer.GetService<IRegionManager>();
        var regionManager2 = App.ServiceProvider.GetService<IRegionManager>();

        // 导航到 Page1View
        regionManager.RequestNavigate("PageRegion", nameof(Page1View));

        var name = App.AppContainer.GetService<IMyService>()?.Name;
        var name2 = App.ServiceProvider.GetService<IMyService>()?.Name;

        var dispatcher = App.AppContainer.GetService<Dispatcher>();
        var configuration = App.AppContainer.GetService<IConfiguration>();
    }

    [RelayCommand]
    public void ShowPage2()
    {
        // 导航到 Page2View
        App.AppContainer.GetService<IRegionManager>()
            .RequestNavigate("PageRegion", nameof(Page2View));

        var eventAggregator = App.GetService<IEventAggregator>();

        eventAggregator.GetEvent<MyBusinessEvent>().Publish(new MyBusinessEventPayload());

        // 不通过 IEventAggregator 的集中管理，也可以自行管理。
        // 目前还不知道具体的场景，在需要做 scope 管理的有用？
        var myBusinessEvent = new MyBusinessEvent();
        myBusinessEvent.Publish(new MyBusinessEventPayload());
        var subscriptionToken = myBusinessEvent.Subscribe(payload => { });
    }
}
